import request from "@/api/request.ts";
import downloadRequest from "@/api/downloadRequest";
import axios from "axios";
import { getToken } from "@/utils/auth";
// 获取机构tree下拉框
export const getDeptSelectTree = (): Promise<apiServerResponse> => {
  return request({
    url: "/sysDept/deptSelectTree",
    method: "GET",
  });
};

// 获取机构列表tree
export const getDeptListByTree = (
  params: SysDeptEntityRequestParams
): Promise<apiServerResponse> => {
  return request({
    url: "/sysDept/deptList",
    method: "GET",
    params,
  });
};

// 新增机构
export const addDept = (data: SysDeptEntity): Promise<apiServerResponse> => {
  return request({
    url: "/sysDept/add",
    method: "POST",
    data,
  });
};

// 修改机构
export const updateDept = (data: SysDeptEntity): Promise<apiServerResponse> => {
  return request({
    url: "/sysDept/update",
    method: "PUT",
    data,
  });
};

// 根据ID删除机构
export const removeDeptById = (id: string): Promise<apiServerResponse> => {
  return request({
    url: `/sysDept/deleteById/${id}`,
    method: "DELETE",
  });
};

// 获取人员列表
export const getUserListByPage = (
  params: SysUserEntityRequestParams
): Promise<apiServerResponse> => {
  return request({
    url: "/sysUser/getUserByPage",
    method: "GET",
    params,
  });
};
// 新增人员
export const addUser = (data: SysUserEntity): Promise<apiServerResponse> => {
  return request({
    url: "/sysUser/add",
    method: "POST",
    data,
  });
};

// 修改人员
export const updateUser = (data: SysUserEntity): Promise<apiServerResponse> => {
  return request({
    url: "/sysUser/update",
    method: "PUT",
    data,
  });
};
// 根据ID删除人员
export const removeUserById = (id: string): Promise<apiServerResponse> => {
  return request({
    url: `/sysUser/deleteById/${id}`,
    method: "DELETE",
  });
};

// 根据ID删除人员
export const removeUserByIds = (data: Array<String>): Promise<apiServerResponse> => {
  return request({
    url: `/sysUser/deleteByIds`,
    method: "DELETE",
    data,
  });
};

// 用户模板下载
export const downloadUserTemplate = () => {
  return axios({
    url: `/proxy/sysUser/exportDefaultUserTemplate`,
    method: "GET",
    responseType: "blob",
    headers: {
      "Content-Type": "application/json;charset=UTF-8",
      Authorization: "Bearer " + getToken() || "",
    },
  }).then((res) => {
    let blob = new Blob([res.data], {
      type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    });
    let downloadElement = document.createElement("a"); //创建一个a 虚拟标签
    let href = window.URL.createObjectURL(blob); // 创建下载的链接
    downloadElement.href = href;
    downloadElement.download = "用户导入模板"; // 下载后文件名
    document.body.appendChild(downloadElement);
    downloadElement.click(); // 点击下载
    document.body.removeChild(downloadElement); // 下载完成移除元素
    window.URL.revokeObjectURL(href); // 释放掉blob对象
  });
};
